package com.zn.utils
{
    import com.zn.events.ScreenScrollEvent;
    
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.display.Stage;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    
    import mx.events.ResizeEvent;

    public class ScreenUtils 
    {
		
		public static var stage:Stage;
		
		
		public static const MAX_W:Number = 1420;
		
		public static const MAX_H:Number = 700;
		
		public static const MIN_W:Number = 1000;
		
		public static const MIN_H:Number = 600;

		
        private static var _currentScrollDisplayObject:DisplayObject;

        private static var _currentScrollRect:Rectangle;

        private static var _lastMousePoint:Point;

		private static var _normalW:Number=0;

		private static var _normalH:Number=0;
		
		private static var _isMark:Boolean=false;
		
		public static var eventDispatcher:EventDispatcher=new EventDispatcher();
		
        public static function addScreenScroll(displayObject:DisplayObject,isMark:Boolean=false):void
        {
            removeScreenScroll();
			
			_isMark=isMark;

            _currentScrollDisplayObject = displayObject;
			
			var sw:Number=stage.stageWidth;
			var sh:Number=stage.stageHeight;
			
            var startX:Number = normalW* 0.5 - sw* 0.5;
            var startY:Number = normalH * 0.5 -sh * 0.5;

            _currentScrollRect = new Rectangle(startX, startY, sw, sh);

			updateScorllRect();

			stage.addEventListener(ResizeEvent.RESIZE,resizeHandler);
            _currentScrollDisplayObject.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }
		
		protected static function resizeHandler(event:ResizeEvent):void
		{
			updateScorllRect();
		}
		
		public static function updateScorllRect():void
		{
			if(_currentScrollDisplayObject==null)
				return ;
			_currentScrollRect.x = Math.min(_currentScrollRect.x, normalW -stage.stageWidth);
			_currentScrollRect.y = Math.min(_currentScrollRect.y, normalH - stage.stageHeight);
		
			_currentScrollRect.x = Math.max(0, _currentScrollRect.x);
			_currentScrollRect.y = Math.max(0, _currentScrollRect.y);
		
			_currentScrollRect.width=stage.stageWidth;
			_currentScrollRect.height=stage.stageHeight;
			
			if(_isMark)
			{
				_currentScrollDisplayObject.scrollRect=_currentScrollRect;
			}
			else
			{
				_currentScrollDisplayObject.x=-_currentScrollRect.x; 
				_currentScrollDisplayObject.y=-_currentScrollRect.y;
			}
			
			eventDispatcher.dispatchEvent(new ScreenScrollEvent(ScreenScrollEvent.SCREEN_SCROLL_EVENT,_currentScrollRect.clone()));
		}

		public static function setScorllRect(rect:Rectangle):void
		{
			_currentScrollRect=rect;
			updateScorllRect();
		}
		
		public static function scrollRectToPoint(p:Point):void
		{
			var rect:Rectangle=new Rectangle();
			rect.width=stage.stageWidth;
			rect.height=stage.stageHeight;
			rect.x=p.x-rect.width*0.5;
			rect.y=p.y-rect.height*0.5;
			setScorllRect(rect);
		}
		
        protected static function mouseDownHandler(event:MouseEvent):void
        {
            _lastMousePoint = new Point(event.stageX, event.stageY);

			stage.addEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
        }

        protected static function stageMouseUpHandler(event:MouseEvent):void
        {
			_lastMousePoint = null;
			(_currentScrollDisplayObject as Sprite).mouseChildren=true;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
        }

        protected static function stageMouseMoveHandler(event:MouseEvent):void
        {
			(_currentScrollDisplayObject as Sprite).mouseChildren=false;
        	var dx:Number=event.stageX-_lastMousePoint.x;
			var dy:Number=event.stageY-_lastMousePoint.y;
			
			_lastMousePoint.x=event.stageX;
			_lastMousePoint.y=event.stageY;
			
			_currentScrollRect.x-=dx;
			_currentScrollRect.y-=dy;
			
			updateScorllRect();
			
        }

        public static function removeScreenScroll():void
        {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, stageMouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUpHandler);
			stage.removeEventListener(ResizeEvent.RESIZE,resizeHandler);
			
            if (_currentScrollDisplayObject)
            {
				(_currentScrollDisplayObject as Sprite).mouseChildren=true;
				_currentScrollDisplayObject.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
				
				_currentScrollDisplayObject.x=_currentScrollDisplayObject.y=0;
                _currentScrollRect = null;
                _lastMousePoint = null;
            }
        }

		public static function get normalW():Number
		{
			return _normalW;
		}

		public static function set normalW(value:Number):void
		{
			_normalW = value;
//			_normalW=Math.min(Main.MAX_W,_normalW);
			_normalW=Math.max(MIN_W,_normalW);
			updateScorllRect();
		}

		public static function get normalH():Number
		{
			return _normalH;
		}

		public static function set normalH(value:Number):void
		{
			_normalH = value;
			
//			_normalH=Math.min(Main.MAX_H,_normalH);
			_normalH=Math.max(MIN_H,_normalH);
			updateScorllRect();
		}
    }
}
